package com.ooqiu.gaming.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.ooqiu.gaming.R;

/**
 * @author Eenie
 * @date 2017/11/16
 */

public class Topbar extends Toolbar {
    private Context mContext;
    private int DEFAULT_LAYOUT;
    private TextView mNavigateBackView;
    private TextView mNavigateTitleView;
    private TextView mNavMenuView;


    private View mCustomLayout;


    private CharSequence mNavDes;
    private CharSequence mNavTitle;
    private CharSequence mNavMenu;
    private Drawable mNavIcon;
    private Drawable mNavBackground;
    private Drawable mNavMenuIcon;
    private int mMinorTextAppearance;
    private int mMainTextAppearance;


    public Topbar(Context context) {
        this(context, null);
    }

    public Topbar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Topbar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init(attrs);
    }


    private void init(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Topbar);
            DEFAULT_LAYOUT = a.getResourceId(R.styleable.Topbar_layout, R.layout.view_default_topbar);
            mNavIcon = generateIcon(a.getResourceId(R.styleable.Topbar_nav_icon, 0));
            mNavMenuIcon = generateIcon(a.getResourceId(R.styleable.Topbar_nav_menu_icon, 0));
            mNavBackground = generateIcon(a.getResourceId(R.styleable.Topbar_nav_background, R.drawable.shape_statuebar));
            mNavDes = a.getString(R.styleable.Topbar_nav_text);
            mNavTitle = a.getString(R.styleable.Topbar_nav_title);
            mNavMenu = a.getString(R.styleable.Topbar_nav_menu_text);
            mMinorTextAppearance = a.getResourceId(R.styleable.Topbar_nav_minor_text_style, R.style.TopBarMinorText_White);
            mMainTextAppearance = a.getResourceId(R.styleable.Topbar_nav_main_text_style, R.style.TopBarMainText_White);
            a.recycle();
        }
    }


    private Drawable generateIcon(int resId) {
        if (resId == 0) {
            return null;
        } else {
            return getResources().getDrawable(resId);
        }
    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mCustomLayout = LayoutInflater.from(mContext).inflate(DEFAULT_LAYOUT, this);
        mNavigateBackView = mCustomLayout.findViewById(R.id.nav_back);
        mNavigateTitleView = mCustomLayout.findViewById(R.id.nav_title);
        mNavMenuView = mCustomLayout.findViewById(R.id.nav_menu);

        setBackground(mNavBackground);

        setNavBack();

        setNavTitle(mNavTitle);

        setNavMenu();
    }

    private void setNavMenu() {
        if (mNavMenuView != null) {
            if (mNavMenuIcon != null) {
                mNavMenuIcon.setBounds(0, 0, mNavMenuIcon.getMinimumWidth(), mNavMenuIcon.getMinimumHeight());
                mNavMenuView.setCompoundDrawables(mNavMenuIcon, null, null, null);
            } else {
                mNavMenuView.setText(mNavMenu);
                mNavMenuView.setTextAppearance(mContext, mMinorTextAppearance);
            }
        }
    }

    private void setNavBack() {
        if (mNavigateBackView != null) {
            setNavIcon(mNavIcon);
            setNavText(mNavDes);
        }
    }


    @Override
    public void setNavigationOnClickListener(OnClickListener listener) {
        setOnNavBackClickListener(listener);
    }

    @Override
    public void setOnMenuItemClickListener(OnMenuItemClickListener listener) {

    }

    @Override
    public void setNavigationContentDescription(@Nullable CharSequence description) {

    }


    @Override
    public void setNavigationIcon(@Nullable Drawable icon) {

    }

    public void setNavIcon(@Nullable Drawable icon) {
        if (icon != null && mNavigateBackView != null) {
            ensureNav();
            icon.setBounds(0, 0, icon.getMinimumWidth(), icon.getMinimumHeight());
            mNavigateBackView.setCompoundDrawables(icon, null, null, null);
        }
    }


    public void setNavText(CharSequence navDes) {
        if (mNavigateBackView != null && !TextUtils.isEmpty(navDes)) {
            ensureNav();
            mNavigateBackView.setText(navDes);
            mNavigateBackView.setTextAppearance(mContext, mMinorTextAppearance);
        }
    }
    public void setMenuText(CharSequence navDes) {
        if (mNavMenuView != null && !TextUtils.isEmpty(navDes)) {
            ensureNav();
            mNavMenuView.setText(navDes);
            mNavMenuView.setTextAppearance(mContext, mMinorTextAppearance);
        }
    }


    public void setNavTitle(CharSequence navTitle) {
        if (mNavigateTitleView != null) {
            mNavigateTitleView.setText(navTitle);
            mNavigateTitleView.setTextAppearance(mContext, mMainTextAppearance);
        }
    }

    private void ensureNav() {
        if (mNavigateBackView != null) {
            mNavigateBackView.setVisibility(VISIBLE);
        }
    }


    public void setOnMenuClickListener(OnClickListener listener) {
        if (mNavMenuView != null) {
            mNavMenuView.setOnClickListener(listener);
        }
    }

    public void setOnNavBackClickListener(OnClickListener listener) {
        if (mNavigateBackView != null) {
            mNavigateBackView.setOnClickListener(listener);
        }
    }
}